home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH8 / SRC / FLAKE2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  6.0 KB  |  216 lines

  1. VERSION 4.00
  2. Begin VB.Form FlakeForm 
  3.    Caption         =   "Snowflake"
  4.    ClientHeight    =   4335
  5.    ClientLeft      =   2280
  6.    ClientTop       =   1185
  7.    ClientWidth     =   5355
  8.    Height          =   5025
  9.    Left            =   2220
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   289
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   357
  14.    Top             =   555
  15.    Width           =   5475
  16.    Begin VB.TextBox LevelText 
  17.       Height          =   285
  18.       Left            =   600
  19.       MaxLength       =   3
  20.       TabIndex        =   0
  21.       Text            =   "4"
  22.       Top             =   0
  23.       Width           =   375
  24.    End
  25.    Begin VB.PictureBox Canvas 
  26.       AutoRedraw      =   -1  'True
  27.       Height          =   4335
  28.       Left            =   1080
  29.       ScaleHeight     =   285
  30.       ScaleMode       =   3  'Pixel
  31.       ScaleWidth      =   281
  32.       TabIndex        =   3
  33.       Top             =   0
  34.       Width           =   4275
  35.    End
  36.    Begin VB.CommandButton CmdGo 
  37.       Caption         =   "Go"
  38.       Default         =   -1  'True
  39.       Enabled         =   0   'False
  40.       Height          =   495
  41.       Left            =   120
  42.       TabIndex        =   1
  43.       Top             =   600
  44.       Width           =   735
  45.    End
  46.    Begin MSComDlg.CommonDialog FileDialog 
  47.       Left            =   240
  48.       Top             =   1440
  49.       _version        =   65536
  50.       _extentx        =   847
  51.       _extenty        =   847
  52.       _stockprops     =   0
  53.       cancelerror     =   -1  'True
  54.    End
  55.    Begin VB.Label Label1 
  56.       Caption         =   "Level"
  57.       Height          =   255
  58.       Index           =   0
  59.       Left            =   0
  60.       TabIndex        =   2
  61.       Top             =   0
  62.       Width           =   495
  63.    End
  64.    Begin VB.Menu mnuFile 
  65.       Caption         =   "&File"
  66.       Begin VB.Menu mnuFileLoad 
  67.          Caption         =   "&Load..."
  68.          Shortcut        =   ^L
  69.       End
  70.       Begin VB.Menu mnuFileSep 
  71.          Caption         =   "-"
  72.       End
  73.       Begin VB.Menu mnuFileExit 
  74.          Caption         =   "E&xit"
  75.       End
  76.    End
  77. Attribute VB_Name = "FlakeForm"
  78. Attribute VB_Creatable = False
  79. Attribute VB_Exposed = False
  80. Option Explicit
  81. Const PI = 3.14159
  82. Dim TheLevel As Integer
  83. Dim StartLength As Integer
  84. ' Coordinates of the points in the initiator.
  85. Dim NumIni As Integer
  86. Dim IniX() As Single
  87. Dim IniY() As Single
  88. ' Angles and distances for the generator.
  89. Dim NumGen As Integer
  90. Dim DistFactor As Single
  91. Dim GenDTheta() As Single
  92. Sub GetParameters()
  93.     If Not IsNumeric(LevelText.Text) Then _
  94.         LevelText.Text = "4"
  95.     TheLevel = CInt(LevelText.Text)
  96. End Sub
  97. ' ************************************************
  98. ' Load a snowflake definition file with format:
  99. '   # Initiator points.
  100. '   (x1, y1)
  101. '   (x2, y2)
  102. '       :
  103. '   DistFactor
  104. '   # Generator angles.
  105. '   theta1
  106. '   theta2
  107. '       :
  108. ' ************************************************
  109. Private Sub mnuFileLoad_Click()
  110. Dim fname As String
  111. Dim fnum As Integer
  112. Dim theta As Single
  113. Dim i As Integer
  114.     ' Allow the user to pick a file.
  115.     On Error Resume Next
  116.     FileDialog.FilterIndex = 1
  117.     FileDialog.filename = "*.SNO"
  118.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  119.     FileDialog.ShowOpen
  120.     If Err.Number = cdlCancel Then
  121.         Exit Sub
  122.     ElseIf Err.Number <> 0 Then
  123.         Beep
  124.         MsgBox "Error selecting file.", , vbExclamation
  125.         Exit Sub
  126.     End If
  127.     On Error GoTo 0
  128.     fname = Trim$(FileDialog.filename)
  129.     FileDialog.InitDir = Left$(fname, Len(fname) _
  130.         - Len(FileDialog.FileTitle) - 1)
  131.     ' Open the file.
  132.     fnum = FreeFile
  133.     Open fname For Input Access Read As #fnum
  134.     ' Read the initiator.
  135.     Input #fnum, NumIni
  136.     ReDim IniX(0 To NumIni)
  137.     ReDim IniY(0 To NumIni)
  138.     For i = 1 To NumIni
  139.         Input #fnum, IniX(i), IniY(i)
  140.     Next i
  141.     IniX(0) = IniX(NumIni)
  142.     IniY(0) = IniY(NumIni)
  143.     ' Read the generator information.
  144.     Input #fnum, DistFactor, NumGen
  145.     ReDim GenDTheta(1 To NumGen)
  146.     For i = 1 To NumGen
  147.         Input #fnum, theta
  148.         GenDTheta(i) = theta * PI / 180
  149.     Next i
  150.     Close #fnum
  151.     Caption = "Snowflake [" & fname & "]"
  152.     CmdGo.Enabled = True
  153. End Sub
  154. ' ************************************************
  155. ' Recursively draw a snowflake edge starting at
  156. ' (x1, y1) in direction theta and distance dist.
  157. ' Leave the coordinates of the endpoint in
  158. ' (x1, y1).
  159. ' ************************************************
  160. Sub DrawFlakeEdge(level As Integer, x1 As Single, y1 As Single, ByVal theta As Single, ByVal dist As Single)
  161. Dim status As Integer
  162. Dim i As Integer
  163. Dim x2 As Single
  164. Dim y2 As Single
  165.     If level <= 0 Then
  166.         x2 = x1 + dist * Cos(theta)
  167.         y2 = y1 + dist * Sin(theta)
  168.         Canvas.Line (x1, y1)-(x2, y2)
  169.         x1 = x2
  170.         y1 = y2
  171.         Exit Sub
  172.     End If
  173.     ' Recursively draw the edge.
  174.     dist = dist * DistFactor
  175.     For i = 1 To NumGen
  176.         theta = theta + GenDTheta(i)
  177.         DrawFlakeEdge level - 1, x1, y1, theta, dist
  178.     Next i
  179. End Sub
  180. Private Sub CmdGo_Click()
  181. Dim i As Integer
  182. Dim x1 As Single
  183. Dim y1 As Single
  184. Dim x2 As Single
  185. Dim y2 As Single
  186. Dim dx As Single
  187. Dim dy As Single
  188. Dim theta As Single
  189.     MousePointer = vbHourglass
  190.     DoEvents
  191.     Canvas.Cls
  192.     ' Get the parameters.
  193.     GetParameters
  194.     ' Draw the snowflake.
  195.     For i = 1 To NumIni
  196.         x1 = IniX(i - 1)
  197.         y1 = IniY(i - 1)
  198.         x2 = IniX(i)
  199.         y2 = IniY(i)
  200.         dx = x2 - x1
  201.         dy = y2 - y1
  202.         theta = Arctan2(dx, dy)
  203.         StartLength = Sqr(dx * dx + dy * dy)
  204.         DrawFlakeEdge TheLevel, x1, y1, _
  205.             theta, StartLength
  206.     Next i
  207.     MousePointer = vbDefault
  208. End Sub
  209. Private Sub Form_Resize()
  210.     Canvas.Move Canvas.Left, 0, _
  211.         ScaleWidth - Canvas.Left, ScaleHeight - 1
  212. End Sub
  213. Private Sub mnuFileExit_Click()
  214.     Unload Me
  215. End Sub
  216.